import { $wuxSelect } from '../../component/wux/index'
var common = require('../../lib/common.js');
var url = require('../../config/url.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    types: {
      value: [],
      title: [],
      options: [
      ],
    },
    // 上传链接
    uploadUrl: url.domain + '/Index/Upload/video',
    content: '',
    videoList: [],
    user: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ id: options.id, title: options.name });
    wx.setNavigationBarTitle({
      title: this.data.title
    })
    this.getType();
    this.checkUserInfo();
  },

  onPullDownRefresh() {
    this.setData({
      content: '',
      videoList: [],
      user: {},
      types: {
        value: [],
        title: [],
        options: [
        ],
      }
    })
    this.getType();
    this.checkUserInfo();
    wx.stopPullDownRefresh();
  },

  getType() {
    let types = {};
    types.value = [];
    types.title = [];
    types.options = [];
    common.post(url.getTwoType, { id: this.data.id }, (res) => {
      for (let i = 0; i < res.length; i++) {
        types.options.push({
          title: res[i].name,
          value: res[i].id + '',
          // color: "#1296db"
        });
      }
      this.setData({ types: types });
    });

  },

  checkUserInfo() {
    common.post(url.getUserInfo, {}, (data) => {
      this.setData({ user: data });
      if (data.mobile == 0) {
        wx.showModal({
          title: '温馨提示',
          content: '您的个人信息暂未完善，请先完善',
          confirmText: '马上完善',
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/user_update/index'
              })
            } else if (res.cancel) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onTypeSelect() {
    var that = this;
    $wuxSelect('#selectTypes').open({
      multiple: true,
      value: that.data.types.value,
      options: that.data.types.options,
      onConfirm: (value, index, options) => {
        let types = that.data.types;
        types.value = value;
        types.title = index.map((n) => options[n].title);
        this.setData({
          types: types,
        })
      },
    });
  },

  onVideoComplete(e) {
    const { fileList } = e.detail
    wx.hideLoading();
  },

  onVideoChange(e) {
    const { file, fileList } = e.detail;
    if (file.status === 'uploading') {
      wx.showLoading({ mask: true, title: '上传中' });
    } else if (file.status === 'done') {
      this.setData({ videoList: fileList });
    }
  },
  onVideoRemove(e) {
    const { file, fileList } = e.detail;
    this.setData({
      videoList: fileList.filter((n) => n.uid !== file.uid),
    })
  },
  onChangeValue(e) {
    let value = e.detail.detail.value;
    this.setData({ content: value });
  },
  onRelease() {
    let params = {};
    let content = this.data.content;
    if (content.length < 5) {
      wx.showModal({
        title: '提示',
        content: '内容字数不能小于5个字',
        success(res) {
        }
      })
      return false;
    }
    let twoTypeId = this.data.types.value;
    if (twoTypeId.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请选择要发布的分类',
        success(res) {
        }
      })
      return false;
    }

    let videos = [];
    videos = this.data.videoList.map((n) => {
      let data = JSON.parse(n.res.data);
      return data.data.url;
    });


    params.oneTypeId = this.data.id;
    params.content = this.data.content;
    twoTypeId.forEach((element, index) => {
      params['twoTypeId[' + index + ']'] = element;
    });
    if (videos.length > 0)
      params['video'] = videos[0];

    let that = this;
    wx.showLoading({ mask: true, title: '发布中' });
    common.post(url.addArticle, params, (res) => {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        content: '发布成功',
        success() {
          if (that.data.user.type == 1) {
            wx.startPullDownRefresh();
          } else {
            wx.redirectTo({
              // url: '/pages/aritcle_manage/index?id=' + res
              url: '/pages/my_aritcle_release/index'
            })
          }
        },
      })
    });
  }


})